﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;

namespace Web
{
    public partial class QueryYearPrice : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                CategoryController categoryController=new CategoryController();
                IList<CategoryEntity> categoryEntities = categoryController.FindAllCategory();
                CategoryDropDownList.DataSource = categoryEntities;
                CategoryDropDownList.DataTextField = "Name";
                CategoryDropDownList.DataValueField = "Id";
                CategoryDropDownList.DataBind();
                this.BindProductDropDownListData();
                this.BindGridViewData();
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }

        protected void ProductDropDwonList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindGridViewData();
        }

        
        protected void CategoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindProductDropDownListData();
            this.BindGridViewData();
        }

        private void BindGridViewData()
        {

            int productId = Convert.ToInt32(ProductDropDwonList.SelectedItem.Value);
            ProductController productController = new ProductController();
            SalesGridView.DataSource = productController.FindYearProductSalesPrice(productId);
            SalesGridView.DataBind();

            PurchaseGridView.DataSource = productController.FindYearProductPurchasePrice(productId);
            PurchaseGridView.DataBind();
        }

        private void BindProductDropDownListData()
        {
            ProductController productController = new ProductController();
            int categoryId =Convert.ToInt32(CategoryDropDownList.SelectedItem.Value);
            IList<ProductEntity> productEntities = productController.FindProductEntitiesByCategory(categoryId);
            ProductDropDwonList.DataSource = productEntities;
            ProductDropDwonList.DataTextField = "No";
            ProductDropDwonList.DataValueField = "Id";
            ProductDropDwonList.DataBind();
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            if(SearchProductNoTextBox.Text.Trim()!="")
            {
                string productNo = SearchProductNoTextBox.Text.Trim();
                ProductController productController = new ProductController();
                ProductEntity productEntity = productController.FindProductByProductNo(productNo);
                if (productEntity!=null)
                {
                    int productId = productEntity.Id;
                    SalesGridView.DataSource = productController.FindYearProductSalesPrice(productId);
                    SalesGridView.DataBind();

                    PurchaseGridView.DataSource = productController.FindYearProductPurchasePrice(productId);
                    PurchaseGridView.DataBind();
                }
                else
                {
                    Session["FlashMessage"] = "Notice: No Such Product. Please Retype Product No.!";
                    Session["FlashMessageType"] = "notice";
                }
            }
            else
            {
                Session["FlashMessage"] = "Notice: Please Input Product No.!";
                Session["FlashMessageType"] = "notice";
            }
        }


    }
}